/***************
 * Source Organizations Panel
 */
AIR2.Source.Organizations = function() {
    var isAdmin = (AIR2.USERINFO.type == "S");

    // MANAGER role in any organization? show the button
    var isManager = isAdmin;
    Ext.iterate(AIR2.USERAUTHZ, function(uuid,role,obj) {
        if (AIR2.AUTHZ.MANAGER & role) isManager = true;
    });    
    
    var p = new AIR2.UI.Panel({
        colspan: 1,
        title: 'Organizations',
        showTotal: true,
        iconCls: 'air2-icon-organization',
        storeData: AIR2.Source.ORGDATA,
        url: AIR2.Source.BASEURL + '/organizations',
        itemSelector: '.air2-source-org',
        tpl: new Ext.XTemplate(
            '<table><tpl for="."><tr class="air2-source-org">' +
              '<td>' +
                '<div class="fullname">{[AIR2.Format.orgNameLong(values.Organization, true)]}</div>' +
              '</td>' +
              '<td style="width:40px">' +
                '<tpl if="so_home_flag"><span class="icons air2-icon-home" ext:qtip="Home Organization"></span></tpl>' +
              '</td>' +
            '</tr></tpl></table>',
            {compiled: true, disableFormats: true}
        ),
        editModal: {
        allowAdd: isManager,
        width: 600,
        items: {
            xtype: 'air2liveeditor',
            url: AIR2.Source.BASEURL + '/organizations',
            sortField: 'org_display_name',
            sortDir: 'ASC',
            pageSize: 50,
            allowEdit: true,
            allowDelete: true,
            hideButtons: true,
            editForm: {
                labelAlign: 'top',
                layout:'column',
                items: [{
                    xtype: 'container',
                    columnWidth: .44,
                    layout: 'form',
                    items: {
                        xtype: 'air2searchbox',
                        id: 'air2-organization-picker',
                        name: 'org_uuid',
                        fieldLabel: 'Organization',
                        searchUrl: AIR2.HOMEURL + '/organization',
                        baseParams: {sort: 'org_display_name', excl_user: AIR2.User.USERUUID},
                        valueField: 'org_uuid',
                        displayField: 'org_display_name',
                        disabled: true,
                        formatComboListItem: function(values) {
                            return values.org_display_name;
                        },
                        listeners: {
                            'render': function(thisBox) {
                                // add listener to filter out orgs the user cannot MANAGE
                                if (!isAdmin) {
                                    thisBox.store.on('load', function(thisStore,recs,opts) {
                                        thisStore.each(function(rec) {
                                            var uuid = rec.get('org_uuid');
                                            if (!(AIR2.AUTHZ.MANAGER & AIR2.USERAUTHZ[uuid])) {
                                                //Logger("filter out non-MANAGER for " + uuid);
                                                thisStore.remove(rec);
                                            }
                                            return true;
                                        });
                                    });
                                }
                            }
                        }
                    }
                },{
                    xtype: 'container',
                    columnWidth: .3,
                    layout: 'form',
                    items: [{
                        xtype: 'checkbox',
                        name: 'so_home_flag',
                        inputType: 'hidden'
                    },{
                        xtype: 'hidden',
                        name: 'so_status',
                        value: 'A'
                    }]
                }],
                listeners: {
                    beforeedit: function(form, rec) {
                        var f = form.getForm();
                        f.setValues(rec.data);
                        var uuidfld = form.find('name', 'org_uuid')[0];
                        uuidfld.setDisabled(!rec.phantom);
                        uuidfld.setRawValue(rec.data.org_display_name);
                    },
                    beforesave: function(form, rec) {
                        var f = form.getForm();
                        f.updateRecord(rec);
                    }
                }
            },
            tpl: new Ext.XTemplate(
                '<div class="air2-source-org" style="width:100%">' +
                  '<div class="flags">' +
                    '<tpl if="so_home_flag"><span class="icons air2-icon-home" ext:qtip="Home Organization"></span></tpl>' +
                  '</div>' +
                  '<div class="org-role">' +
                    '<span class="fullname">{[AIR2.Format.userOrgLong(values)]}</span>' +
                  '</div>' +
                  '<span class="meta">' +
                    '<span>{[AIR2.Format.userOrgShort(values)]}</span>' +
                    '<span class="datewho">Added on {[AIR2.Format.dateLong(values.so_cre_dtim)]} ' +
                      'by {[AIR2.Format.userName(values.CreUser, true)]}</span>' +
                  '</span>' +
                  '<tpl if="this.canEdit(values)">' +
                    '<button class="air2-rowedit"></button>' +
                    '<button class="air2-rowdelete"></button>' +
                  '</tpl>' +
                '</div>',
                {
                    compiled: true,
                    disableFormats: true,
                    canEdit: function(values) {
                        if (isAdmin) return true;

                        var role = AIR2.USERAUTHZ[values.org_uuid];
                        if (AIR2.AUTHZ.MANAGER & role) {
                            return true;
                        }
                        else {
                            return false;
                        }
                    }
                }
            )
        }
    }

    });
    
    return p;
}
